package com.sikaryofficial.workbench.utils.excel.listener;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;
import java.util.StringJoiner;

public class ValidateUtil {

    private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    public static String validateEntity(Object obj, Class<?>... groups) {
        if (obj == null) {
            return null;
        }
        Set<ConstraintViolation<Object>> validate = validator.validate(obj, groups);
        if (validate == null || validate.isEmpty()) {
            return null;
        }
        StringJoiner stringJoiner = new StringJoiner(";");
        for (ConstraintViolation<Object> violation : validate) {
            stringJoiner.add(violation.getMessage());
        }
        return stringJoiner.toString();
    }
}
